// let morefunBase = "http://beastworse.tpddns.cn:8085/"; // longgang local
let morefunBase = "https://lamer-creamapi.morefun.me/"; // longgang local

let token = "";

function updateToken(inToken) {
  token = inToken;
}

// wx登录 换token
function getOpenIdByCode(inCode) {
  return `${morefunBase}api/common/getOpenIdByCode/${inCode}`
}

// /api/getRoomList/{openId}
function getRoomList(inId) {
  return `${morefunBase}api/getRoomList/${inId}`
}

// /api/getRoomInfo/{roomNo}
function getRoomInfo(inId, openId) {
  return `${morefunBase}api/getMyRoomInfo/${openId}/${inId}`
}

// 点赞
function userAgree(openId, videoId) {
  return `${morefunBase}api/userAgree/${openId}/${videoId}`
}

// 分享
function userShare(openId, videoId) {
  return `${morefunBase}api/userShare/${openId}/${videoId}`
}

// 观看视频
function userCompleteVideo(openId, videoId) {
  return `${morefunBase}api/userCompleteVideo/${openId}/${videoId}`
}

// 保存用户故事
function saveRoom5Story(openId) {
  return `${morefunBase}api/saveRoom5Story/${openId}`
}

// 查看故事详情
function getStoryById(storyId) {
  return `${morefunBase}api/getStoryById/${storyId}`
}

// 查看自己的故事
function getStoryList(openId) {
  return `${morefunBase}api/getStoryList/${openId}`
}


// 生成二维码
function genQrcode() {
  return `${morefunBase}api/common/genQrcode`
}


// 5.获取指定空间当前ICON任务情况
function getRoomIconTaskInfo(openId, roomNo) {
  return `${morefunBase}api/getRoomIconTaskInfo/${openId}/${roomNo}`
}

// 2.3.用户信息获取
function getUserInfo(openId) {
  return `${morefunBase}api/getUserInfo/${openId}`
}

// 11.查看盲盒故事
function getBoxStory(openId) {
  return `${morefunBase}api/getBoxStory/${openId}`
}

// 2.4.关注微信
function attentionWx(openId) {
  return `${morefunBase}api/attentionWx/${openId}`
}

// 14.用户保存奖品邮寄地址
function saveAddress(openId, userAwardId) {
  return `${morefunBase}api/saveAddress/${openId}/${userAwardId}`
}

// 12.1抽奖礼盒信息
function getAwardList(openId) {
  return `${morefunBase}api/getAwardList/${openId}`
}

// 12.1抽奖礼盒信息
function draw(openId, resourceType) {
  return `${morefunBase}/api/draw/${openId}/${resourceType}`
}

// 16.用户领取转增奖品
function getGivenPrize(openId, resourceType) {
  return `${morefunBase}/api/getGivenPrize/${openId}/${resourceType}`
}

// 15.用户转赠奖品
function givePrize(openId, userAwardId) {
  return `${morefunBase}/api/givePrize/${openId}/${userAwardId}`
}

// oss上的图片墙配置文件
function getPhotoWallData() {
  return `https://morefun-active-1.oss-cn-shanghai.aliyuncs.com/2022_lamer_cream_show/r5_photowall/photoWallDataJson.json?t=1`
}

// 11.1.查看盲盒故事
function getBoxInfo(openId) {
  return `${morefunBase}/api/getBoxInfo/${openId}`
};

// 埋点
function addUserStat() {
  return `${morefunBase}/api/common/addUserStat`
}

function uploadFile(inBizPath, inLocalFilePath) {
  return new Promise(function (resolve, reject) {
    wx.uploadFile({
      url: `${morefunBase}api/common/uploadFile/${inBizPath}`,
      filePath: inLocalFilePath,
      name: 'file',
      success(res) {
        console.log("api --- http --- res ")
        console.log(res)
        if (res.statusCode == "200") {
          resolve(JSON.parse(res.data))
        } else {
          wx.hideLoading();
          wx.showToast({
            title: '请检查网络连接。',
            icon: 'none'
          })
          reject(res);
        }
      },
      fail: err => {
        wx.hideLoading();
        wx.showToast({
          title: '请检查网络连接。',
          icon: 'none'
        })
        reject(err);
      }
    })
  })
}


function http(inUrl, inMethod, inData) {
  return new Promise(function (resolve, reject) {
    wx.request({
      url: inUrl, //仅为示例，并非真实的接口地址
      method: inMethod,
      data: inData,
      header: {
        'content-type': 'application/json',
        'X-Access-Token': token
      },

      // 'Authorization'] = 'Bearer ' + token;

      success: res => {
        // console.log("api --- http --- res ",res)
        if (res.statusCode == "200") {
          resolve(res.data)
        } else {
          wx.hideLoading();
          wx.showToast({
            title: '请检查网络连接。',
            icon: 'none'
          })
          reject(res);
        }
      },
      fail: err => {
        wx.hideLoading();
        wx.showToast({
          title: '请检查网络连接。',
          icon: 'none'
        })
        reject(err);
      }
    })
  });
}

module.exports = {
  updateToken,

  getOpenIdByCode,
  getRoomList,
  getRoomInfo,
  userCompleteVideo,

  userAgree,
  userShare,
  saveRoom5Story,
  getStoryById,
  getStoryList,
  genQrcode,
  getRoomIconTaskInfo,
  getUserInfo,
  getBoxStory,
  attentionWx,
  saveAddress,
  getAwardList,
  draw,
  getGivenPrize,
  givePrize,
  getPhotoWallData,
  addUserStat,
  getBoxInfo,

  http: http,
  uploadFile: uploadFile
};

// module.exports = {
//         formatTime: formatTime
// };